package com.zygh.interactiveplatform.module.service;


import com.zygh.interactiveplatform.module.entity.User;
import com.zygh.interactiveplatform.module.entity.UserRole;
import com.zygh.interactiveplatform.module.mapper.UserMapper;
import com.zygh.interactiveplatform.security.CustomUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class JwtUserDetailsService implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserRoleService userRoleService;


    @Autowired
    private HttpServletRequest request;

    @Override
    public UserDetails loadUserByUsername(String value) throws UsernameNotFoundException {

        User userAccount = userMapper.findUserByUsername(value);

        if (userAccount == null) {
            throw new UsernameNotFoundException(String.format("No user found with username '%s'.", value));
        }

        List<UserRole> roles = userRoleService.findAllNoPage();

        return new CustomUserDetails(userAccount, roles);
    }
}
